package com.baoai.util;

import com.baoai.common.IdName;
import com.baoai.dto.BaseDto;
import org.codehaus.jackson.map.ObjectMapper;
import org.json.JSONObject;
import org.springframework.util.StringUtils;
import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;
import sun.swing.StringUIClientPropertyKey;

import java.io.IOException;
import java.io.StringReader;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/4/9 0009.
 */
public class BaseUtil {

    /**
     * 数组转json
     *
     * @param srcArrays
     * @return
     */
    public static String arrays2Json(String[] srcArrays) {
        if (srcArrays != null && srcArrays.length > 0) {
            return JSONObject.valueToString(srcArrays);
        }
        return null;
    }

    public static String getIdName(Class cls){
        String result = null;
        java.lang.reflect.Field[] sourceFields = cls.getDeclaredFields();
        for (Field field : sourceFields){
            if(field.isAnnotationPresent(IdName.class)){
                result = field.getName();
                break;
            }
        }
        return result;
    }

    public static String getIdValueByName(Object obj){
        String result = null;
        java.lang.reflect.Field[] sourceFields = obj.getClass().getDeclaredFields();
        for (Field field : sourceFields){
            if(field.isAnnotationPresent(IdName.class)){
                result = (String) getFieldValueByName(field.getName(), obj);
                break;
            }
        }
        return result;
    }


    /**
     * json转数组
     *
     * @param json
     * @return
     * @throws IOException
     */
    public static String[] json2Arrays(String json) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        if (json != null && json.length() > 0) {
            return mapper.readValue(json, String[].class);
        }
        return null;
    }

    public static void copySource2Target(BaseDto source, BaseDto target){
        java.lang.reflect.Field[] sourceFields = source.getClass().getDeclaredFields();
        for (Field field : sourceFields){
            Object value = getFieldValueByName(field.getName(), source);
            if(!StringUtils.isEmpty(value)){
                setFieldValueByName(field.getName(), field.getType(), value, target);
            }
        }
    }

    /**
     * 根据属性名获取属性值
     * */
    public static Object getFieldValueByName(String fieldName, Object source) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String getter = "get" + firstLetter + fieldName.substring(1);
            Method method = source.getClass().getMethod(getter, new Class[] {});
            Object value = method.invoke(source, new Object[] {});// get方法获取值
            return value;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 根据属性名获取属性值
     * */
    public static void setFieldValueByName(String fieldName,
                                           Class fieldClass, Object value, Object source) {
        try {
            String firstLetter = fieldName.substring(0, 1).toUpperCase();
            String setter = "set" + firstLetter + fieldName.substring(1);
//            System.out.println(setter + "," + source);
            Method method = source.getClass().getMethod(setter,
                    new Class[] { fieldClass });
            method.invoke(source, new Object[] { value });// set方法获取值
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 使用IK分词器将词语分组
     *
     * @param text
     * @return
     * @throws IOException
     */
    public static String[] ikSeqWord(String text) throws IOException {
        StringReader sr = null;
        List<String> list = null;
        String[] array = null;
        try {
            sr = new StringReader(text);
            IKSegmenter ik = new IKSegmenter(sr, true);
            Lexeme lex = null;
            list = new ArrayList<>();
            while ((lex = ik.next()) != null) {
                list.add(lex.getLexemeText());
            }
            array = new String[list.size()];
        } finally {
            if(sr != null){
                sr.close();
            }
        }
        return list.toArray(array);
    }


}
